package com.example.pei.ithinklearnt.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.bean.Comment;
import com.example.pei.ithinklearnt.utils.ToastUtil;
import com.example.pei.ithinklearnt.view.customview.JustifyTextView;
import com.makeramen.roundedimageview.RoundedImageView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.displayImage;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption2;

public class CommentAdapter extends BaseAdapter {

    private Context context;
    private List<Comment> data;

    public CommentAdapter(Context c, List<Comment> data){
        this.context = c;
        this.data = data;
    }


    @Override
    public int getCount() {
        return (data==null?0:data.size());
    }

    @Override
    public Comment getItem(int i) {
        return data.get(i);
    }



    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        // 重用convertView
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment, null);

            holder.textView_sendName=convertView.findViewById(R.id.tv_sendname);
            holder.textView_sendContent=convertView.findViewById(R.id.tv_sendcontent);
            holder.textView_sendTime=convertView.findViewById(R.id.tv_sendtime);
            holder.roundedImageView_head=convertView.findViewById(R.id.img_roudhead);
            holder.textView_replayto=convertView.findViewById(R.id.tv_replayto);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        // 适配数据



        holder.textView_sendName.setText(data.get(i).getUserFrom().getNickName());
        holder.textView_sendContent.setText(data.get(i).getContent());
        holder.textView_sendTime.setText(getSendTime(data.get(i).getCreatedAt()));

        //回复的动态
        if(data.get(i).getUserTo().getObjectId().equals(data.get(i).getDynamic().getSender().getObjectId())
                &&(data.get(i).getBcontent()==null||data.get(i).getBcontent()=="")){
            holder.textView_replayto.setVisibility(View.GONE);
        }else {
            holder.textView_replayto.setVisibility(View.VISIBLE);
            holder.textView_replayto.setText("回复@" + data.get(i).getUserTo().getNickName() + "的评论：" + data.get(i).getBcontent());
        }
        //设置评论者头像
        displayImage(context,holder.roundedImageView_head,data.get(i).getUserFrom().getHeadFileUrl(),getPhotoImageOption2());

        return convertView;
    }

    /**
     * 添加一条评论,刷新列表
     * @param comment
     */
    public void addComment(Comment comment){
        if(data==null){
            data=new ArrayList<Comment>();
        }
        data.add(comment);
        notifyDataSetChanged();
    }

    public void clearComment(){
        if(data!=null){
            data.clear();
        }
    }

    public void deleteComment(int position){
        if(data!=null){
            data.remove(position);
        }
    }

    /**
     * 静态类，便于GC回收
     */
    public static class ViewHolder{

        TextView textView_sendName;             //评论者id
        JustifyTextView textView_sendContent;   //评论者内容
        TextView textView_sendTime;             //评论时间
        RoundedImageView roundedImageView_head; //评论者头像
        TextView textView_replayto;             //回复的人（可有可无，没有的话直接盖楼）


    }


    private String getSendTime(String fullTime){
        String s=null;
        Calendar c = Calendar.getInstance();
        int currentYear = c.get(Calendar.YEAR);
        int currentmonth = c.get(Calendar.MONTH);
        int currentday = c.get(Calendar.DATE);
        Date fulldate;
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            fulldate = sdf.parse(fullTime);
            if(currentYear!=fulldate.getYear()+1900){
                //不是今年发的技能，显示全时间
                s=(fulldate.getYear()+1900)+"年"+(fulldate.getMonth()+1)+"月"+fulldate.getDate()+"日"+fulldate.getHours()+"时";
            }else if(currentmonth!=fulldate.getMonth()){
                //是今年 但不是这个月发的
                s=fulldate.getMonth()+"月"+fulldate.getDate()+"日"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }else if(currentday-fulldate.getDate()==0){
                //是今年是这个月差一天
                s="今天"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            } else if(currentday-fulldate.getDate()==1){
                //是今年是这个月差一天
                s="昨天"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }else if(currentday-fulldate.getDate()==2){
                //是今年是这个月差一天
                s="前天"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }else {
                s=(fulldate.getMonth()+1)+"月"+fulldate.getDate()+"日"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }
        } catch (ParseException ee) {
            ee.printStackTrace();
        }

        return s;
    }
}
